﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CSP.Dao.IObjectDaos.CSP;
using CSP.Dao.CommonDao;
using CSP.DataAccess;
using CSP.Model.CSP;

namespace CSP.Dao.ObjectDaos.CSP
{
    public class ServiceAssetDao : CSPCommonDao,IServiceAssetDao
    {
        public ServiceAssetDao(CSPContext context, bool isTransaction = false)
            : base(context, isTransaction)
        { }

        public ICollection<Model.CSP.ServiceAsset> FindAll()
        {
            return _context.ServiceAssets.ToArray();
        }


        public ICollection<ServiceAsset> FindByParentId(int parentId)
        {
            var result = _context.ServiceAssets.Where(s => s.Parentid == parentId);

            if (result.Count() != 0)
            {
                return result.ToArray();
            }
            else
            {
                return null;
            }
        }

        public Model.CSP.ServiceAsset GetDataById(int id)
        {
            return _context.ServiceAssets.FirstOrDefault(a => a.Id == id);
        }

        public ICollection<Model.CSP.ServiceAsset> GeDatatByIds(int[] Ids)
        {
            return _context.ServiceAssets.Where(a => Ids.Contains(a.Id)).ToArray();
        }

        public void Add(Model.CSP.ServiceAsset obj)
        {
            _context.ServiceAssets.Add(obj);
            _context.SaveChanges();
        }

        public void Update(Model.CSP.ServiceAsset obj)
        {
            ServiceAsset serviceArea = GetDataById(obj.Id);
            if (serviceArea != null)
            {
                serviceArea = obj;
            }

            _context.SaveChanges();
        }

        public ServiceAsset DeleteById(int id)
        {
            ServiceAsset serviceArea = GetDataById(id);
            _context.ServiceAssets.Remove(serviceArea);
            _context.SaveChanges();

            return serviceArea;
        }

    }
}
